/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T011Interaction;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T010Substance;

/**
 *
 * @author Spyros
 */
public class T011InteractionJpaController {

    public T011InteractionJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T011Interaction t011Interaction) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T010Substance t010Substance = t011Interaction.getT010Substance();
            if (t010Substance != null) {
                t010Substance = em.getReference(t010Substance.getClass(), t010Substance.getId());
                t011Interaction.setT010Substance(t010Substance);
            }
            T010Substance t010Substance1 = t011Interaction.getT010Substance1();
            if (t010Substance1 != null) {
                t010Substance1 = em.getReference(t010Substance1.getClass(), t010Substance1.getId());
                t011Interaction.setT010Substance1(t010Substance1);
            }
            em.persist(t011Interaction);
            if (t010Substance != null) {
                t010Substance.getT011InteractionCollection().add(t011Interaction);
                t010Substance = em.merge(t010Substance);
            }
            if (t010Substance1 != null) {
                t010Substance1.getT011InteractionCollection().add(t011Interaction);
                t010Substance1 = em.merge(t010Substance1);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T011Interaction t011Interaction) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T011Interaction persistentT011Interaction = em.find(T011Interaction.class, t011Interaction.getId());
            T010Substance t010SubstanceOld = persistentT011Interaction.getT010Substance();
            T010Substance t010SubstanceNew = t011Interaction.getT010Substance();
            T010Substance t010Substance1Old = persistentT011Interaction.getT010Substance1();
            T010Substance t010Substance1New = t011Interaction.getT010Substance1();
            if (t010SubstanceNew != null) {
                t010SubstanceNew = em.getReference(t010SubstanceNew.getClass(), t010SubstanceNew.getId());
                t011Interaction.setT010Substance(t010SubstanceNew);
            }
            if (t010Substance1New != null) {
                t010Substance1New = em.getReference(t010Substance1New.getClass(), t010Substance1New.getId());
                t011Interaction.setT010Substance1(t010Substance1New);
            }
            t011Interaction = em.merge(t011Interaction);
            if (t010SubstanceOld != null && !t010SubstanceOld.equals(t010SubstanceNew)) {
                t010SubstanceOld.getT011InteractionCollection().remove(t011Interaction);
                t010SubstanceOld = em.merge(t010SubstanceOld);
            }
            if (t010SubstanceNew != null && !t010SubstanceNew.equals(t010SubstanceOld)) {
                t010SubstanceNew.getT011InteractionCollection().add(t011Interaction);
                t010SubstanceNew = em.merge(t010SubstanceNew);
            }
            if (t010Substance1Old != null && !t010Substance1Old.equals(t010Substance1New)) {
                t010Substance1Old.getT011InteractionCollection().remove(t011Interaction);
                t010Substance1Old = em.merge(t010Substance1Old);
            }
            if (t010Substance1New != null && !t010Substance1New.equals(t010Substance1Old)) {
                t010Substance1New.getT011InteractionCollection().add(t011Interaction);
                t010Substance1New = em.merge(t010Substance1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t011Interaction.getId();
                if (findT011Interaction(id) == null) {
                    throw new NonexistentEntityException("The t011Interaction with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T011Interaction t011Interaction;
            try {
                t011Interaction = em.getReference(T011Interaction.class, id);
                t011Interaction.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t011Interaction with id " + id + " no longer exists.", enfe);
            }
            T010Substance t010Substance = t011Interaction.getT010Substance();
            if (t010Substance != null) {
                t010Substance.getT011InteractionCollection().remove(t011Interaction);
                t010Substance = em.merge(t010Substance);
            }
            T010Substance t010Substance1 = t011Interaction.getT010Substance1();
            if (t010Substance1 != null) {
                t010Substance1.getT011InteractionCollection().remove(t011Interaction);
                t010Substance1 = em.merge(t010Substance1);
            }
            em.remove(t011Interaction);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T011Interaction> findT011InteractionEntities() {
        return findT011InteractionEntities(true, -1, -1);
    }

    public List<T011Interaction> findT011InteractionEntities(int maxResults, int firstResult) {
        return findT011InteractionEntities(false, maxResults, firstResult);
    }

    private List<T011Interaction> findT011InteractionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T011Interaction.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T011Interaction findT011Interaction(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T011Interaction.class, id);
        } finally {
            em.close();
        }
    }

    public int getT011InteractionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T011Interaction> rt = cq.from(T011Interaction.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
